﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Config.cs" company="Prolific Notion">
//   (c) Copyright 2010 Simon Dingley t/a Prolific Notion, http://prolificnotion.co.uk
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

#region License
// --------------------------------------------------------------------------------------------------------------------
// Distrbuted under the MIT License
// --------------------------------------------------------------------------------------------------------------------

// (c) Copyright 2010 Simon Dingley t/a Prolific Notion, http://prolificnotion.co.uk

// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all copies or substantial portions of 
// the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// --------------------------------------------------------------------------------------------------------------------
#endregion

namespace ProlificNotion.Umbraco.ImportMedia
{
    using System.Collections.Generic;
    using System.Web;
    using System.Xml;

    /// <summary>The config.</summary>
    public static class Config
    {
        #region Constants and Fields

        /// <summary>The config file.</summary>
        private const string ConfigFile = "~/config/importMedia.config";

        /// <summary>The xml config doc.</summary>
        private static readonly XmlDocument XmlConfigDoc;

        #endregion

        #region Constructors and Destructors

        /// <summary>Initializes static members of the <see cref="Config"/> class. 
        /// static lazy load constructor</summary>
        static Config()
        {
            XmlConfigDoc = new XmlDocument();
            XmlConfigDoc.Load(HttpContext.Current.Server.MapPath(ConfigFile));
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets the import folders
        /// </summary>
        /// <returns>A list of folders to use for the imports</returns>
        public static List<string> ImportFolders
        {
            get
            {
                var folders = new List<string>();

                foreach (XmlNode node in XmlConfigDoc.GetElementsByTagName("folder"))
                {
                    folders.Add(node.InnerText);
                }

                return folders;
            }
        }

        /// <summary>Gets ThumbnailMaxWidthHeight.</summary>
        public static int ThumbnailMaxWidthHeight
        {
            get
            {
                var maxWidthHeight = 100;

                XmlNode node = XmlConfigDoc.SelectSingleNode("/images");
                if (node != null)
                {
                    if (node.Attributes != null)
                    {
                        // Attempt to parse the thumbnailMaxWidthHeight value 
                        int.TryParse(node.Attributes["thumbnailMaxWidthHeight"].InnerText, out maxWidthHeight);
                    }
                }

                return maxWidthHeight;
            }
        }

        #endregion
    }
}